﻿@using Microsoft.AspNetCore.Components.Authorization
@using MauiHybrid.Authentication
@inject AuthenticationStateProvider authStateProvider
<div class="top-row ps-3 navbar navbar-dark">
    <div class="container-fluid">
        <a class="navbar-brand" href="">
            <AuthorizeView>
                <Authorized>
                    <h6>Hi, @context.User.Identity.Name</h6>
                </Authorized>
                <NotAuthorized>
                    <h6>Hi, Guest</h6>
                </NotAuthorized>
            </AuthorizeView>
        </a>
        <button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>
</div>

<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
    <nav class="flex-column">
        <AuthorizeView Roles="Admin">
            <Authorized>
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                        <span class="oi oi-home" aria-hidden="true"></span> Manage User
                    </NavLink>
                </div>
            </Authorized>
        </AuthorizeView>
        
        <AuthorizeView>
            <Authorized>
                <div class="nav-item px-3">
                    <NavLink class="nav-link" style="cursor:pointer" @onclick="LogoutAsync">
                        <span class="oi oi-plus" aria-hidden="true"></span> Logout
                    </NavLink>
                    
                </div>
            </Authorized>
            <NotAuthorized>
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="login">
                        <span class="oi oi-list-rich" aria-hidden="true"></span> Login
                    </NavLink>
                </div>
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="register">
                        <span class="oi oi-list-rich" aria-hidden="true"></span> Register
                    </NavLink>
                </div>
            </NotAuthorized>
        </AuthorizeView>
        <AuthorizeView Roles="User">
            <Authorized>
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="update-profile">
                        <span class="oi oi-list-rich" aria-hidden="true"></span> Update Profile
                    </NavLink>
                </div>
            </Authorized>
        </AuthorizeView>
       
    </nav>
</div>

@code {
    private bool collapseNavMenu = true;

    private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;

    private void ToggleNavMenu()
    {
        collapseNavMenu = !collapseNavMenu;
    }

    public async Task LogoutAsync()
    {
        var customAuthStateProvider = (CustomAuthenticationStateProvider)authStateProvider;
        await customAuthStateProvider.UpdateAuthenticationState(new UserSession());
    }
}
